$(function () {
    $("#jqGrid").jqGrid({
        url: '../businesspay/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 30, key: true ,hidden:true},
            { label: '商保异动明细id', name: 'yddetid', index: 'yddetid', width: 80  ,hidden:true},
            { label: '批次号', name: 'batchcode', index: 'batchcode', width: 80 },
            { label: '结算点id', name: 'settlementid', index: 'settlementid', width: 80  ,hidden:true},
            { label: '结算点', name: 'settlementname', index: 'settlementname', width: 80 },
			{ label: '姓名', name: 'name', index: 'name', width: 60 },
			{ label: '身份证号', name: 'idnumber', index: 'idnumber', width: 70 },
			{ label: '性别', name: 'chsex', index: 'chsex', width: 40 ,formatter: getDictChsex},
			{ label: '年龄', name: 'age', index: 'age', width: 40 },
			{ label: '保险类型', name: 'insurancetype', index: 'insurancetype', width: 80 ,formatter: getDictInfo},
            { label: '保险金额', name: 'insuranceamount', index: 'insuranceamount', width: 80 },
			{ label: '保险编号', name: 'insuranceid', index: 'insuranceid', width: 80 },
			{ label: '录入人员', name: 'inputperson', index: 'inputperson', width: 80 }, 			
			{ label: '录入日期', name: 'inputdate', index: 'inputdate', width: 80 }, 			
			{ label: '一审日期', name: 'auditdate', index: 'auditdate', width: 80 },
			{ label: '一审人', name: 'auditperson', index: 'auditperson', width: 60 },
            { label: '付款状态', name: 'pay', index: 'pay', width:80 ,formatter: getPay},
			{ label: '二审人', name: 'fsr', index: 'fsr', width: 60 },
			{ label: '二审日期', name: 'fstime', index: 'fstime', width: 80 },
            { label: '审批状态', name: 'audit', index: 'audit', width: 80 ,formatter:getaudit,unformat:ungetaudit }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 30,
		rowList : [10,30,50],
        rownumbers: true,
        autowidth:true,
        shrinkToFit:false,
        autoScroll: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    var r = utils.getDictList("socialSecurity,sex");
    vm.insurancetype = r.socialSecurity;
    vm.chsex = r.sex;
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            name: null,
            insurancetype: null
        },
		showList: true,
		title: null,
		businesspay:{},
        insurancetype:{},
		pay:{},
		chsex:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.businesspay = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.audit!='0'){
                alert("已走审批的数据不允许修改！");
                return;
			}
				vm.showList = false;
       		     vm.title = "修改";
                vm.getInfo(id);

		},
		saveOrUpdate: function (event) {
			var url = vm.businesspay.id == null ? "../businesspay/save" : "../businesspay/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.businesspay),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			for(var i=0;i<ids.length;i++){
                var rowData = $("#jqGrid").jqGrid('getRowData',ids[i]);
                if(rowData.audit!='0'){
                    alert("姓名："+rowData.name+"已经做过审批操作！");
                    return;
                }
            }

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../businesspay/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../businesspay/info/"+id, function(r){
                vm.businesspay = r.businesspay;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    name: vm.q.name,
                    insurancetype: vm.q.insurancetype
                },
                page:page
            }).trigger("reloadGrid");
		},
        firstaudit:function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
			if(rowData.audit!='0'){
				alert("该条记录已在审批中或者还未到达初审节点！");
				return;
			}
            utils.openAuditView("../businesspay/simpleaudit",1,id,$("#jqGrid"));
        },
		secondaudit:function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.audit=='1'){
                utils.openAuditView("../businesspay/simpleaudit",2,id,$("#jqGrid"));
            }else{
                alert("该条记录已在审批中或者还未到复审节点！");
                return;
			}
		}
	}
});
function getDictChsex(cellvalue, options, rowObject){
    var html="";
    for(var i=0; i<vm.chsex.length; i++){
        if(vm.chsex[i].value==cellvalue){
            html=vm.chsex[i].label;
        }
        if(html==undefined){
            html="";
        }
    }
    return html;
}
function getPay(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="0"){
        html="未付款";
    }else if(cellvalue=="1"){
        html="付款中";
    }else if(cellvalue=="2"){
        html="已付款";
    }
    return html;
}

function getaudit(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="0"){
        html="未审核";
    }else if(cellvalue=="1"){
        html="一级已审核";
    }else if(cellvalue=="2"){
        html="二级已审核";
    }else if(cellvalue=="3"){
        html="三级已审核";
    }
    return html;
}
function ungetaudit(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="未审核"){
        html="0";
    }else if(cellvalue=="一级已审核"){
        html="1";
    }else if(cellvalue=="二级已审核"){
        html="2";
    }else if(cellvalue=="三级已审核"){
        html="3";
    }
    return html;
}